package toy.keli.edic.ui.main.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp


// 可按需扩展或自定义此预设颜色列表
val presetColors = listOf(
    Color.Red,  Color.Blue,  Color.Cyan,Color.Green,Color.Yellow,
)

@Composable
@Preview
fun SingleRowColorPicker(nowColor:Color,title:String="当前选中颜色: ",onChange:(c:Color)->Unit={}) {
    var selectedColor by remember { mutableStateOf(nowColor) }

    Row(
        modifier = Modifier.fillMaxWidth(),
//        horizontalAlignment = Alignment.Start
    ) {
        Text(text = title, modifier = Modifier.padding(end = 10.dp))
        // 单行颜色选择器
        LazyRow(
//            modifier = Modifier.padding(top = 6.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            items(presetColors) { color ->
                Box(
                    modifier = Modifier
                        .size(20.dp)
                        .clip(CircleShape)
                        .background(color)
                        .clickable {
                            selectedColor = color
                            onChange(color)
                        }
                        .then(
                            if (color == selectedColor) {
                                Modifier.border(1.dp, Color.Black, CircleShape)
                            } else {
                                Modifier
                            }
                        )
                )
            }
        }
    }
}